/**
 * Created by Administrator on 2017/2/28 0028.
 */

//模块定义
var gulp = require('gulp'),
    autoprefixer = require('gulp-autoprefixer'),
    cache = require('gulp-cache'),
    clean = require('gulp-clean'),
    cleancss = require('gulp-clean-css'),
    concat = require('gulp-concat'),
    imagemin = require('gulp-imagemin'),
    plumber = require('gulp-plumber'),
    rename = require('gulp-rename'),
    sass = require('gulp-sass'),
    notify = require('gulp-notify'),
    sourcemaps = require('gulp-sourcemaps'),
    uglify = require('gulp-uglify'),
    minifycss = require('gulp-minify-css'),
    spritesmith = require('gulp.spritesmith');



//复制文件到发布环境中
gulp.task('copy',  function() {
    return gulp.src('src/**/*')
        .pipe(gulp.dest('dist'))
});

//复制html文件
gulp.task('copyHtml',  function() {
    return gulp.src('src/html/*.html')
        .pipe(gulp.dest('dist/html'))
});

// 样式的编译和压缩
gulp.task('styles', function() {
    return gulp.src('src/css/*.css')
        .pipe(sass({ style: 'expanded'}))
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
        .pipe(concat('main.js'))
        .pipe(gulp.dest('dist/css'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(minifycss())
        .pipe(gulp.dest('dist/css'))
        .pipe(notify({ message: 'Styles task complete' }));
});

// javaScript脚本处理
gulp.task('scripts', function() {
    return gulp.src('src/js/*.js')
        .pipe(concat('main.js'))
        .pipe(gulp.dest('dist/js'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(gulp.dest('dist/js'))
        .pipe(notify({ message: 'Scripts task complete' }));
});

// 图片压缩
gulp.task('images', function() {
    return gulp.src('src/img/*.{png,jpg,gif}')
        .pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
        .pipe(gulp.dest('dist/img'))
        .pipe(notify({ message: 'Images task complete' }));
});

// 清理
gulp.task('clean', function() {
    return gulp.src(['dist/css/*', 'dist/js/*', 'dist/img/*','dist/html/*'], {read: false})
        .pipe(clean());
});

// 预设任务
gulp.task('default', ['clean'], function() {
    gulp.start('styles', 'scripts', 'images');
});





















